package com.wrench.elevator.install.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import wrench.elevator.install.util.JPushUtil;
import wrench.elevator.install.util.WrenchElevatorInstallException;
import wrench.elevator.install.util.WrenchElevatorInstallUtil;

import com.alibaba.fastjson.JSON;
import com.wrench.elevator.install.constant.Result;
import com.wrench.elevator.install.domain.Building;
import com.wrench.elevator.install.service.BuildingService;
import com.wrench.elevator.install.service.DanyuanService;
import com.wrench.elevator.install.service.ElevatorService;
import com.wrench.elevator.install.service.UserService;
import com.wrench.elevator.install.service.ZhuangService;

@Controller
@RequestMapping(value = "/app")
public class AppController {
	private static final Logger logger = LoggerFactory
			.getLogger(AppController.class);

	@Resource
	private UserService userService;

	@Resource
	private BuildingService buildingService;

	@Resource
	private ZhuangService zhuangService;

	@Resource
	private DanyuanService danyuanService;

	@Resource
	private ElevatorService elevatorService;

	/**
	 * 登录
	 */
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public void login(HttpServletRequest request, HttpServletResponse response,
			ModelMap map) {
		logger.debug("==>**********enter in login(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
		String tel = WrenchElevatorInstallUtil.getPara(request, "tel");
		String pwd = WrenchElevatorInstallUtil.getPara(request, "pwd");
		logger.debug("==> params : tel = {} , pwd = {}   ", tel, pwd);
		WrenchElevatorInstallException error = null;

		try {
			userService.userLogin(tel, pwd, map);
		} catch (WrenchElevatorInstallException e) {
			error = e;
		}
		if (null == error) {

			map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
			map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
			map.put("message", "login success");

		} else {

			map.put(Result.RESULT_CODE, error.getResultCode());
			map.put(Result.RESULT_MESSAGE, error.getResultMessage());
		}
		String responseStr = JSON.toJSONString(map);
		logger.debug("==> responseStr = {}", responseStr);
		WrenchElevatorInstallUtil.responseStr(response, responseStr);
		map = null;
		logger.debug("==>**********exit out login(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
	}

	/**
	 * 添加员工
	 */
	@RequestMapping(value = "/addEmp", method = RequestMethod.POST)
	public void addEmp(HttpServletRequest request,
			HttpServletResponse response, ModelMap map) {
		logger.debug("==>**********enter in addEmp(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
		String user_id = WrenchElevatorInstallUtil.getPara(request, "user_id");
		String power = WrenchElevatorInstallUtil.getPara(request, "power");
		String user_name = WrenchElevatorInstallUtil.getPara(request,
				"user_name");
		String user_pwd = WrenchElevatorInstallUtil
				.getPara(request, "user_pwd");
		String user_tel = WrenchElevatorInstallUtil
				.getPara(request, "user_tel");
		String company_id = WrenchElevatorInstallUtil.getPara(request,
				"company_id");
		logger.debug(
				"==> params : user_id = {} , power = {} , user_name = {} , user_pwd = {} ,user_tel = {} , company_id = {}  ",
				user_id, power, user_name, user_pwd, user_tel, company_id);
		WrenchElevatorInstallException error = null;

		try {
			userService.addEmp(user_id, power, user_name, user_pwd, user_tel,
					company_id, map);
		} catch (WrenchElevatorInstallException e) {
			error = e;
		}
		map.clear();
		if (null == error) {

			map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
			map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
			map.put("message", "add success");

		} else {

			map.put(Result.RESULT_CODE, error.getResultCode());
			map.put(Result.RESULT_MESSAGE, error.getResultMessage());
		}
		String responseStr = JSON.toJSONString(map);
		logger.debug("==> responseStr = {}", responseStr);
		WrenchElevatorInstallUtil.responseStr(response, responseStr);
		map = null;
		logger.debug("==>**********exit out addEmp(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
	}

	/**
	 * 删除
	 */
	@RequestMapping(value = "/delEmp", method = RequestMethod.POST)
	public void delEmp(HttpServletRequest request,
			HttpServletResponse response, ModelMap map) {
		logger.debug("==>**********enter in delEmp(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
		String user_id = WrenchElevatorInstallUtil.getPara(request, "user_id");
		String power = WrenchElevatorInstallUtil.getPara(request, "power");
		String emp_id = WrenchElevatorInstallUtil.getPara(request, "emp_id");
		logger.debug("==> params : user_id = {} , power = {} , emp_id = {}  ",
				user_id, power, emp_id);
		WrenchElevatorInstallException error = null;

		try {
			userService.delEmp(user_id, power, emp_id, map);
		} catch (WrenchElevatorInstallException e) {
			error = e;
		}
		if (null == error) {

			map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
			map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
			map.put("message", "del emp success");

		} else {

			map.put(Result.RESULT_CODE, error.getResultCode());
			map.put(Result.RESULT_MESSAGE, error.getResultMessage());
		}
		String responseStr = JSON.toJSONString(map);
		logger.debug("==> responseStr = {}", responseStr);
		WrenchElevatorInstallUtil.responseStr(response, responseStr);
		map = null;
		logger.debug("==>**********exit out login(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
	}

	/**
	 * 加载员工列表
	 */
	@RequestMapping(value = "/loadEmpList", method = RequestMethod.GET)
	public void loadEmpList(HttpServletRequest request,
			HttpServletResponse response, ModelMap map) {
		logger.debug("==>**********enter in loadEmpList(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
		String user_id = WrenchElevatorInstallUtil.getPara(request, "user_id");
		String power = WrenchElevatorInstallUtil.getPara(request, "power");
		logger.debug("==> params : user_id = {} , power = {}   ", user_id,
				power);
		WrenchElevatorInstallException error = null;

		try {
			userService.loadEmpList(user_id, power, map);
		} catch (WrenchElevatorInstallException e) {
			error = e;
		}
		if (null == error) {

			map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
			map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
			map.put("message", "load empList success");

		} else {

			map.put(Result.RESULT_CODE, error.getResultCode());
			map.put(Result.RESULT_MESSAGE, error.getResultMessage());
		}
		String responseStr = JSON.toJSONString(map);
		logger.debug("==> responseStr = {}", responseStr);
		WrenchElevatorInstallUtil.responseStr(response, responseStr);
		map = null;
		logger.debug("==>**********exit out loadEmpList(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");

	}

	/**
	 * 员工第一次登录修改密码
	 */
	@RequestMapping(value = "/updatePwdFirst", method = RequestMethod.POST)
	public void updatePwdFirst(HttpServletRequest request,
			HttpServletResponse response, ModelMap map) {
		logger.debug("==>**********enter in updatePwdFirst(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
		String user_id = WrenchElevatorInstallUtil.getPara(request, "user_id");
		String power = WrenchElevatorInstallUtil.getPara(request, "power");
		String new_pwd = WrenchElevatorInstallUtil.getPara(request, "new_pwd");
		logger.debug("==> params : user_id = {} , power = {} , new_pwd = {}  ",
				user_id, power, new_pwd);
		WrenchElevatorInstallException error = null;

		try {
			userService.updatePwdFirst(user_id, power, new_pwd);
		} catch (WrenchElevatorInstallException e) {
			error = e;
		}
		if (null == error) {

			map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
			map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
			map.put("message", "update pwd success");

		} else {

			map.put(Result.RESULT_CODE, error.getResultCode());
			map.put(Result.RESULT_MESSAGE, error.getResultMessage());
		}
		String responseStr = JSON.toJSONString(map);
		logger.debug("==> responseStr = {}", responseStr);
		WrenchElevatorInstallUtil.responseStr(response, responseStr);
		map = null;
		logger.debug("==>**********exit out updatePwdFirst(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");

	}

	/**
	 * 修改密码
	 */
	@RequestMapping(value = "/updatePwd", method = RequestMethod.POST)
	public void updatePwd(HttpServletRequest request,
			HttpServletResponse response, ModelMap map) {
		logger.debug("==>**********enter in updatePwd(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
		String tel = WrenchElevatorInstallUtil.getPara(request, "tel");
		String new_pwd = WrenchElevatorInstallUtil.getPara(request, "new_pwd");
		logger.debug("==> params : tel = {} ,  new_pwd = {}  ", tel, new_pwd);
		WrenchElevatorInstallException error = null;

		try {
			userService.updatePwd(tel, new_pwd);
		} catch (WrenchElevatorInstallException e) {
			error = e;
		}
		if (null == error) {

			map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
			map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
			map.put("message", "update pwd success");

		} else {

			map.put(Result.RESULT_CODE, error.getResultCode());
			map.put(Result.RESULT_MESSAGE, error.getResultMessage());
		}
		String responseStr = JSON.toJSONString(map);
		logger.debug("==> responseStr = {}", responseStr);
		WrenchElevatorInstallUtil.responseStr(response, responseStr);
		map = null;
		logger.debug("==>**********exit out updatePwd(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");

	}

	/**
	 * 加载小区列表
	 */
	@RequestMapping(value = "/locaBuildingList", method = RequestMethod.GET)
	public void locaBuildingList(HttpServletRequest request,
			HttpServletResponse response, ModelMap map) {
		logger.debug("==>**********enter in locaBuildingList(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
		String user_id = WrenchElevatorInstallUtil.getPara(request, "user_id");
		String power = WrenchElevatorInstallUtil.getPara(request, "power");
		logger.debug("==> params : user_id = {} ,  power = {}  ", user_id,
				power);
		WrenchElevatorInstallException error = null;
		List<Building> buildingList = null;
		try {
			buildingList = buildingService.loadBuildingList(user_id, power);
		} catch (WrenchElevatorInstallException e) {
			error = e;
		}

		if (null == error) {

			map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
			map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
			map.put("buildings", buildingList);
			map.put("message", "load building list success");

		} else {

			map.put(Result.RESULT_CODE, error.getResultCode());
			map.put(Result.RESULT_MESSAGE, error.getResultMessage());
		}
		String responseStr = JSON.toJSONString(map);
		logger.debug("==> responseStr = {}", responseStr);
		WrenchElevatorInstallUtil.responseStr(response, responseStr);
		map = null;
		logger.debug("==>**********exit out locaBuildingList(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");

	}

	/**
	 * 根据小区ID加载幢列表
	 */
	@RequestMapping(value = "/loadZhuangList", method = RequestMethod.GET)
	public void loadZhuangList(HttpServletRequest request,
			HttpServletResponse response, ModelMap map) {
		logger.debug("==>**********enter in loadZhuangList(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
		String building_id = WrenchElevatorInstallUtil.getPara(request,
				"building_id");
		logger.debug("==> params :  building_id = {}  ", building_id);
		WrenchElevatorInstallException error = null;

		try {
			zhuangService.loadZhuangList(building_id, map);
		} catch (WrenchElevatorInstallException e) {
			error = e;
		}

		if (null == error) {

			map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
			map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
			map.put("message", "load zhuang list success");

		} else {

			map.put(Result.RESULT_CODE, error.getResultCode());
			map.put(Result.RESULT_MESSAGE, error.getResultMessage());
		}
		String responseStr = JSON.toJSONString(map);
		logger.debug("==> responseStr = {}", responseStr);
		WrenchElevatorInstallUtil.responseStr(response, responseStr);
		map = null;
		logger.debug("==>**********exit out loadZhuangList(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");

	}

	/**
	 * 根据幢ID加载单元列表
	 */
	@RequestMapping(value = "/loadDanyuanList", method = RequestMethod.GET)
	public void loadDanyuanList(HttpServletRequest request,
			HttpServletResponse response, ModelMap map) {
		logger.debug("==>**********enter in loadDanyuanList(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
		String zhuang_id = WrenchElevatorInstallUtil.getPara(request,
				"zhuang_id");
		logger.debug("==> params :  zhuang_id = {}  ", zhuang_id);
		WrenchElevatorInstallException error = null;
		try {
			danyuanService.loadDanyuanList(zhuang_id, map);
		} catch (WrenchElevatorInstallException e) {
			error = e;
		}

		if (null == error) {

			map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
			map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
			map.put("message", "load danyuan list success");

		} else {

			map.put(Result.RESULT_CODE, error.getResultCode());
			map.put(Result.RESULT_MESSAGE, error.getResultMessage());
		}
		String responseStr = JSON.toJSONString(map);
		logger.debug("==> responseStr = {}", responseStr);
		WrenchElevatorInstallUtil.responseStr(response, responseStr);
		map = null;
		logger.debug("==>**********exit out loadDanyuanList(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");

	}

	/**
	 * 激活电梯
	 */
	@RequestMapping(value = "/activetionElevator", method = RequestMethod.POST)
	public void activetionElevator(HttpServletRequest request,
			HttpServletResponse response, ModelMap map) {
		logger.debug("==>**********enter in activetionElevator(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
		String province_id = WrenchElevatorInstallUtil.getPara(request,
				"province_id");
		String province_name = WrenchElevatorInstallUtil.getPara(request,
				"province_name");
		String city_id = WrenchElevatorInstallUtil.getPara(request, "city_id");
		String city_name = WrenchElevatorInstallUtil.getPara(request,
				"city_name");
		String district_id = WrenchElevatorInstallUtil.getPara(request,
				"district_id");
		String district_name = WrenchElevatorInstallUtil.getPara(request,
				"district_name");
		String building_id = WrenchElevatorInstallUtil.getPara(request,
				"building_id");
		String building_name = WrenchElevatorInstallUtil.getPara(request,
				"building_name");
		String zhuang_id = WrenchElevatorInstallUtil.getPara(request,
				"zhuang_id");
		String zhuang_name = WrenchElevatorInstallUtil.getPara(request,
				"zhuang_name");
		String danyuan_id = WrenchElevatorInstallUtil.getPara(request,
				"danyuan_id");
		String danyuan_name = WrenchElevatorInstallUtil.getPara(request,
				"danyuan_name");
		String device_alias = WrenchElevatorInstallUtil.getPara(request,
				"device_alias");
		String start_loucen = WrenchElevatorInstallUtil.getPara(request,
				"start_loucen");
		String end_loucen = WrenchElevatorInstallUtil.getPara(request,
				"end_loucen");
		String user_id = WrenchElevatorInstallUtil.getPara(request, "user_id");
		String power = WrenchElevatorInstallUtil.getPara(request, "power");

		logger.debug(
				"==> params :  province_id = {} , province_name = {} ,  city_id = {} city_name = {} , district_id = {} , district_name = {} , building_id = {} , building_name = {} , zhuang_id = {} , zhuang_name = {} , danyuan_id = {} , danyuan_name = {} , device_alias = {} , start_loucen = {} , end_loucen = {} , power = {} ",
				province_id, province_name, city_id, city_name, district_id,
				district_name, building_id, building_name, zhuang_id,
				zhuang_name, danyuan_id, danyuan_name, device_alias,
				start_loucen, end_loucen, user_id, power);
		WrenchElevatorInstallException error = null;

		try {
			elevatorService.activationElevator(province_id, province_name,
					city_id, city_name, district_id, district_name,
					building_id, building_name, zhuang_id, zhuang_name,
					danyuan_id, danyuan_name, device_alias, start_loucen,
					end_loucen, user_id, power, map);
		} catch (WrenchElevatorInstallException e) {
			error = e;
		}
		logger.debug("==>   error = {} ",error);

		if (null == error) {

			logger.debug("==> {success}");
			map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
			map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
			map.put("message", "activetion elevator success");
		} else {

			map.put(Result.RESULT_CODE, error.getResultCode());
			map.put(Result.RESULT_MESSAGE, error.getResultMessage());
		}
		String responseStr = JSON.toJSONString(map);
		logger.debug("==> responseStr = {}", responseStr);
		WrenchElevatorInstallUtil.responseStr(response, responseStr);
		map = null;
		logger.debug("==>**********exit out activetionElevator(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
	}

	/**
	 * 设置楼层名称和信息
	 */
	@RequestMapping(value = "/setElevatorLoucenInfo", method = RequestMethod.POST)
	public void setElevatorLoucenInfo(HttpServletRequest request,
			HttpServletResponse response, ModelMap map) {
		logger.debug("==>**********enter in setElevatorLoucenInfo(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
		String elevator_id = WrenchElevatorInstallUtil.getPara(request,
				"elevator_id");
		String loucen_info = WrenchElevatorInstallUtil.getPara(request,
				"loucen_info");
		logger.debug("==> params :  elevator_id = {} , loucen_info = {} ",
				elevator_id, loucen_info);
		WrenchElevatorInstallException error = null;
		try {
			elevatorService.setElevatorInfo(elevator_id, loucen_info);
		} catch (WrenchElevatorInstallException e) {
			error = e;
		}
		String device_alias=null;
		try {
			device_alias=elevatorService.getDeviceAlias(elevator_id);
		} catch (WrenchElevatorInstallException e) {
			// TODO Auto-generated catch block
			error=e;
		}
		

		if (null == error) {

			map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
			map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
			map.put("message", "set elevator loucen info success");
			
			
			Map<String, Object> contents=new HashMap<>();
			contents.put("pushCode", "200");
			contents.put("pushMessage", "success");
			contents.put("loucen_info", loucen_info);
			logger.debug("===> contents => {} , device_alais = {} ",JSON.toJSONString(contents),device_alias);
			JPushUtil.pushToJiaoxiang(contents, device_alias);

		} else {

			map.put(Result.RESULT_CODE, error.getResultCode());
			map.put(Result.RESULT_MESSAGE, error.getResultMessage());
		}
		String responseStr = JSON.toJSONString(map);
		logger.debug("==> responseStr = {}", responseStr);
		WrenchElevatorInstallUtil.responseStr(response, responseStr);
		map = null;
		logger.debug("==>**********exit out setElevatorLoucenInfo(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");

	}

	/**
	 * 查看电梯按键详情
	 */
	@RequestMapping(value = "/loadElevatorLoucenInfo", method = RequestMethod.GET)
	public void loadElevatorLoucenInfo(HttpServletRequest request,
			HttpServletResponse response, ModelMap map) {
		logger.debug("==>**********enter in loadElevatorLoucenInfo(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
		String elevator_id = WrenchElevatorInstallUtil.getPara(request,
				"elevator_id");
		logger.debug("==> params :  elevator_id = {} ", elevator_id);
		WrenchElevatorInstallException error = null;
		String loucen_info = null;
		try {
			loucen_info = elevatorService.locaElevatorLoucenInfo(elevator_id);
		} catch (WrenchElevatorInstallException e) {
			error = e;
		}

		if (null == error) {

			map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
			map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
			map.put("loucen_info", loucen_info);
			map.put("message", "load elevator loucen info success");

		} else {

			map.put(Result.RESULT_CODE, error.getResultCode());
			map.put(Result.RESULT_MESSAGE, error.getResultMessage());
		}
		String responseStr = JSON.toJSONString(map);
		logger.debug("==> responseStr = {}", responseStr);
		WrenchElevatorInstallUtil.responseStr(response, responseStr);
		map = null;
		logger.debug("==>**********exit out loadElevatorLoucenInfo(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");

	}

	/**
	 * 获取电梯列表
	 */
	@RequestMapping(value = "/loadElevatorList", method = RequestMethod.GET)
	public void loadElevatorList(HttpServletRequest request,
			HttpServletResponse response, ModelMap map) {
		logger.debug("==>**********enter in loadElevatorList(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
		String user_id = WrenchElevatorInstallUtil.getPara(request, "user_id");
		String power = WrenchElevatorInstallUtil.getPara(request, "power");
		logger.debug("==> params :  user_id = {} , power = {}", user_id, power);
		WrenchElevatorInstallException error = null;

		try {
			elevatorService.loadElevatorList(user_id, power, map);
		} catch (WrenchElevatorInstallException e) {
			error = e;
		}

		if (null == error) {

			map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
			map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
			map.put("message", "load elevator list success");

		} else {

			map.put(Result.RESULT_CODE, error.getResultCode());
			map.put(Result.RESULT_MESSAGE, error.getResultMessage());
		}
		String responseStr = JSON.toJSONString(map);
		logger.debug("==> responseStr = {}", responseStr);
		WrenchElevatorInstallUtil.responseStr(response, responseStr);
		map = null;
		logger.debug("==>**********exit out loadElevatorList(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");

	}

}